import pygame

SCREEN_SIZE = (480, 700)


class GameSprite(pygame.sprite.Sprite):
    def __init__(self, file_name, speed=1):
        super().__init__()
        self.image = pygame.image.load(file_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):
        self.rect.y += self.speed

class BackGroundSprte(GameSprite):
    def __init__(self, is_alt=False):
        super().__init__('./image/background.png')
        if is_alt:
            self.rect.y = - SCREEN_SIZE[1]

    def update(self):
        super().update()
        if self.rect.y>=SCREEN_SIZE[1]:
            self.rect.y = -SCREEN_SIZE[1]



class PlaneGame:
    def __init__(self):
        """初始化"""
        pygame.init()
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption("飞机大战")
        self.clock = pygame.time.Clock()
        self.is_loop = True
        self._create_sprite()

    def start_game(self):
        """开始游戏"""
        while self.is_loop:
            self._event_handler()
            self.clock.tick(40)
            self.update_sprite()
            pygame.draw.circle(self.screen, (255,0,0),(240, 600), 20)
            pygame.display.update()
        pygame.quit()

    def _create_sprite(self):
        #
        bg1 = BackGroundSprte()
        bg2 = BackGroundSprte(True)
        self.bg_group = pygame.sprite.Group(bg1, bg2)

    def update_sprite(self):
        """更新精灵"""
        self.bg_group.update()
        self.bg_group.draw(self.screen)

    def _event_handler(self):
        """事件"""
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False

plane = PlaneGame()
plane.start_game()


